﻿using System;
using System.Collections.Generic;

using System.Text;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Framework;
using NLBS.Views;
using NLBS.Views.Admin;
using System.Web;
using System.Xml;

namespace NLBS.Web.UI
{
    public class SettingCommand : ICommand, IAction<Setting>
    {

        #region Presenter

        private SettingPresenter _presenter;

        public SettingPresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new SettingPresenter();
                this._presenter.Action = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            this.Presenter.OnActionLoaded();
        }
        #endregion

        private string[] parameters;
        public SettingCommand(string[] parameters)
        {
            this.parameters = parameters;
        }

        #region ICommand Members

        public void Excute()
        {
            PresenterInit();
            switch (parameters[0])
            {
                case "updateLink":
                    updateLink();
                    break;
                case "updateAnnounce":

                    updateAnnounce();
                    break;
                case "updateSettings":
                    updateSettings();
                    break;
                case "changeTheme":
                    this.Entity = new Setting();
                    this.Entity.Name = "styleSheet";
                    this.DataSelectSingle(null, null);
                    this.Entity.Value1 = "/styles/" +RequestWrapper.GetFormString("themeName") + "/styles.css";
                    this.DataUpdate(null, null);
                    break;

                case "closedSite":
                    HttpContext.Current.Application.Lock();
                    HttpContext.Current.Application["NlbsSiteClosed"] = true;
                    HttpContext.Current.Application.UnLock();
                    break;
                case "openSite":
                    HttpContext.Current.Application.Lock();
                    HttpContext.Current.Application["NlbsSiteClosed"] = null;
                    HttpContext.Current.Application.UnLock();
                    break;
                case "clearCache":
                    ContentCache.Instantiate().Clear();
                    break;
            }
        }

        private void updateSettings()
        {
            string[] setnames = new string[41] { "blogTitle", "blogDescription", "baseURL","logoImage","blogWebMaster",
                              "blogWebMasterEmail","blogLanguage","defaultViewMode","articlePerPageNormal",
                              "articlePerPageList","listEntryPerPage","commentPerPage","commentTimeOrder",
                      "showTrackbackWithComment","showTrackbackPosition","recentArticleList","recentCommentList","enableDynamicCalendar",
                      "enableContentAutoSplit","contentAutoSplitChars","enableRegister","enableSecurityCode","enableTrackbackIn","enableTrackbackOut",
                      "enableComment","maxCommentLength","maxCommentURLCount","enableGuestBook","entryPerPageGuestBook",
                      "styleSheet","imageFolder","smiliesFolder","smiliesPerRow","minPostDuration","enableVisitorRecord",
                      "maxVisitorRecord","enableUpload","uploadSize","uploadPath","uploadTypes","foot" };
            for (int s = 0; s < setnames.Length; s++)
            {
                this.Entity = new Setting();
                this.Entity.Name = setnames[s];
                this.DataSelectSingle(null, null);
                Setting updateSets = this.Entity;
                try
                {
                    
                    if (updateSets.Type == 0)
                    {
                        updateSets.Value0 = Convert.ToInt32(parameters[s + 1]);
                    }
                    else
                    {
                        updateSets.Value1 = parameters[s + 1];
                    }
                    this.DataUpdate(null, null);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }

        private void updateAnnounce()
        {
            string[] names = new string[3] { "announce", "announceDate", "announceShow" };
            string[] values = new string[3] { parameters[2], DateTime.Now.ToString(), parameters[1] };
            for (int n = 0; n < names.Length; n++)
            {
                this.Entity = new Setting();
                this.Entity.Name = names[n];
                this.DataSelectSingle(null, null);
                Setting updateSet = this.Entity;
                if (n == 2)
                {
                    updateSet.Value0 = Convert.ToInt32(values[n]);
                }
                else
                {
                    updateSet.Value1 = values[n];
                }
                this.DataUpdate(null, null);
            }
        }

        private void updateLink()
        {
            this.Entity = new Setting();
            this.Entity.Name = "links";
            //this.DataSelectSingle(null, null);
            this.Entity.Value1 = parameters[1];
            XmlDocument doc= SiteConfig.GetLinksXml();
            XmlNode root= doc.DocumentElement;
            foreach (XmlNode item in root.ChildNodes)
            {
                if (item.Attributes["LinkType"].Value == "Menu")
                {
                    item.FirstChild.Value = parameters[1];
                }
                if (item.Attributes["LinkType"].Value == "FriendLink")
                {
                    item.FirstChild.Value = parameters[2];
                }
            }
            
            doc.Save(HttpContext.Current.Server.MapPath("/App_Data/Links.xml"));
           
        }

        #endregion

        #region IAction<Setting> Members

        private Setting _entity;
        public Setting Entity
        {
            get
            {
                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private int _keyID;
        public int keyID
        {
            get
            {
                return _keyID;
            }
            set
            {
                _keyID = value;
            }
        }

        public event EventHandler DataInsert;

        public event EventHandler DataUpdate;

        public event EventHandler DataDelete;

        public event EventHandler DataSelectSingle;

        #endregion
    }
}
